#ifndef REAR_QUEUE_HPP
#define REAR_QUEUE_HPP

#include "LinkQueue.hpp"
#include <iostream>
#include <ostream>

typedef struct
{
    QueuePtr rear;
} rearQueue;

class RearQueue : public LinkQueueC {
private:
    rearQueue q;

public:
    RearQueue()
    {
        initRearQueue();
    }

    Status initRearQueue()
    {
        q.rear = new QNode;
        q.rear->next = q.rear;
        return Status::Ok;
    }

    bool rearQueueEmpty()
    {
        return q.rear->next == q.rear;
    }

    Status enRearQueue(QElemType e)
    {
        QueuePtr s = new QNode;
        s->data = e;
        s->next = q.rear->next;
        q.rear->next = s;
        q.rear = s;
        return Status::Ok;
    }

    Status deRearQueue(QElemType& e)
    {
        if (rearQueueEmpty()) {
            std::cout << "RearQueue is empty, Failed!!!" << std::endl;
            return Status::Err;
        }
        QueuePtr front = q.rear->next->next;
        e = front->data;
        q.rear->next->next = front->next;
        if (front == q.rear) {
            q.rear = q.rear->next;
        }
        delete front;
        return Status::Ok;
    }

    Status clearRearQueue()
    {
        std::cout << "clear queue" << std::endl;
        while (!rearQueueEmpty()) {
            QElemType e;
            deRearQueue(e);
        }
        return Status::Ok;
    }
};

#endif
